<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文章编辑 - XYZ博客</title>
    <meta name="keywords" content="XYZ博客,文章更新编辑器" />
    <meta name="description" content="XYZ博客的文章更新编辑器。" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link href="https://cdn.staticfile.org/layui/2.7.6/css/layui.css" rel="stylesheet">
    <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
    <style>
        #editor—wrapper {
            border: 1px solid #ccc;
            z-index: 100; /* 按需定义 */
        }
        #toolbar-container { border-bottom: 1px solid #ccc; }
        #editor-container { height: 800px; }
    </style>
</head>
<body>
    <ul class="layui-nav layui-bg-green" lay-filter="">
        <li class="layui-nav-item"><a href="/">首页</a></li>
        <li class="layui-nav-item layui-this"><a th:href="'/article/updateArticlePage?id=' + ${article.id}">编辑文章</a></li>
        <li class="layui-nav-item"><a href="/article/newArticlePage">发表新文章</a></li>
        <li class="layui-nav-item"><a href="/pear/index.html">后台管理</a></li>
        <li class="layui-nav-item"><a href="/user/personalPage">个人空间</a></li>
    </ul>

    <blockquote class="layui-elem-quote">
        本页面推荐使用 平板/电脑 进行操作。访问密码可以无需设置，设置之后需要输入访问密码才能查看文章详情内容。<br />
        文章访问密码请设置为6个字符，例如：lh8888。
    </blockquote>

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">文章标题</label>
                <div class="layui-input-block">
                    <input type="text" th:value="${article.title}" id="title" name="title" lay-verify="title" autocomplete="off" placeholder="请输入文章标题" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">标签名字</label>
                <div class="layui-input-block">
                    <input type="text" id="content" name="content" lay-verify="title" autocomplete="off" placeholder="请输入标签名字（新增）" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">标签名字</label>
                <div class="layui-input-inline">
                    <select name="label-langhai" id="label-langhai" lay-verify="required" lay-search="">
                        <option value="">直接选择或搜索选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">访问密码</label>
                <div class="layui-input-block">
                    <input type="text" th:value="${article.password}" id="password" name="password" lay-verify="password" autocomplete="off" placeholder="请输入文章访问密码" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">是否公开</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="public" lay-skin="switch" lay-text="公开|不公开">
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">摘要</label>
            <div class="layui-input-block">
                <textarea th:text="${article.abstractText}" id="abstractText" placeholder="请输入摘要 小于等于30个字 必填" class="layui-textarea"></textarea>
            </div>
        </div>
    </form>

    <div id="editor—wrapper">
        <div id="toolbar-container"><!-- 工具栏 --></div>
        <div id="editor-container"><!-- 编辑器 --></div>
    </div>

    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-radius" id="temporaryUpdates">临时更新文章</button>
        <button type="button" class="layui-btn layui-btn-radius" id="publish">更新文章</button>
    </div>

    <blockquote class="layui-elem-quote">
        <ul class="menu">
            <li>
                &copy; XYZ博客. All rights reserved.
                个人网站 2021 ~ 2029年
            </li>
        </ul>
    </blockquote>

</body>
<script src="https://cdn.staticfile.org/layui/2.7.6/layui.js"></script>
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
<script src="/blogs/assets/js/jquery.min.js"></script>
<script th:inline="javascript">
    //注意：导航 依赖 element 模块，否则无法进行功能性操作
    layui.use(['element', 'form'], function(){
        var element = layui.element;
        var form = layui.form;

        var label = [[${labelList}]];
        var labelContent = [[${article.labelContent}]];
        var publicShow = [[${article.publicShow}]];

        for(var i = 0; i < label.length; i++){
            $('#label-langhai').append('<option value=' + label[i] + '>'+ label[i] +'</option>');
        }

        $("#label-langhai").find("option[value=" + labelContent + "]").attr("selected", true);

        if(1 == publicShow){
            $('input:checkbox:first').attr("checked",'checked');
        }

        form.render();
    });

    $(function () {
        $("#publish").on("click", function () {
            $.ajax({
                url: "/article/updateArticle",
                async: true,
                type: "POST",
                contentType : "application/json",
                dataType : "json",
                data: JSON.stringify({"html": editor.getHtml(),
                    "title": $("#title").val(),
                    "content": $("#content").val(),
                    "label": $("#label-langhai option:selected").text(),
                    "publicShow": $("input[name='public']:checked").val(),
                    "id": [[${article.id}]],
                    "abstractText": $("#abstractText").val(),
                    "password": $("#password").val()
                }),
                success: function (data) {
                    if(data.code == 200){
                        layer.msg(data.message, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            window.location.href = "/article/articleShowNew?id=" + [[${article.id}]];
                        })
                    }else {
                        layer.msg(data.message, {
                            icon: 2,
                            time: 2000
                        });
                    }
                }
            })
        });
        // 临时更新文章
        $("#temporaryUpdates").on("click", function () {
            $.ajax({
                url: "/article/updateArticle",
                async: true,
                type: "POST",
                contentType : "application/json",
                dataType : "json",
                data: JSON.stringify({"html": editor.getHtml(),
                    "title": $("#title").val(),
                    "content": $("#content").val(),
                    "label": $("#label-langhai option:selected").text(),
                    "publicShow": $("input[name='public']:checked").val(),
                    "topFlag": $("input[name='topFlag']:checked").val(),
                    "id": [[${article.id}]],
                    "abstractText": $("#abstractText").val(),
                    "password": $("#password").val()
                }),
                success: function (data) {
                    if(data.code == 200){
                        layer.msg(data.message, {
                            icon: 1,
                            time: 2000
                        })
                    }else {
                        layer.msg(data.message, {
                            icon: 2,
                            time: 2000
                        });
                    }
                }
            })
        });
    })
</script>
<script th:inline="javascript">
    const { createEditor, createToolbar } = window.wangEditor

    function imageDispose(res){
        $("img[src=' " + res.data.url + " ']").css("width", "100%");
        const html = editor.getHtml()
        console.log(html)
        editor.setHtml(html)
    }

    const editorConfig = {
        onChange(editor) {
            const html = editor.getHtml()
            // console.log('editor content', html)
            // 也可以同步到 <textarea>
        },
        MENU_CONF: {
            uploadImage: {
                server: '/minio/upload/wangEditor',

                fieldName: 'file',

                onSuccess(file, res) {
                    if(res.errno == 0 ){
                        layer.msg('文件上传成功', {
                            closeBtn: 1,
                            icon: 6,
                            time: 3 * 1000,
                            offset: '21px'
                        });
                        return
                    }else {
                        layer.msg('文件上传失败', {
                            closeBtn: 1,
                            icon: 2,
                            time: 3 * 1000,
                            offset: '21px'
                        });
                        return
                    }
                }
            }
        }
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: [[${article.html}]],
        config: editorConfig,
        mode: 'default', // or 'simple'
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    })

    $(function () {
        editor.getHtml()
    })
</script>
</html>